package com.sdcqjy.property.activity;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.ActivityOptionsCompat;
import android.support.v4.content.LocalBroadcastManager;
import android.support.v4.util.Pair;
import android.support.v4.view.ViewCompat;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.sdcqjy.mylibrary.widget.PublicDialog;
import com.sdcqjy.mylibrary.widget.photoView.RoundedImageView;
import com.sdcqjy.property.AppLL;
import com.sdcqjy.property.MyConstants;
import com.sdcqjy.property.R;
import com.sdcqjy.property.base.SimpleBaseActivity;
import com.sdcqjy.property.mode.LoginMode;
import com.sdcqjy.property.presenter.SelfMsgPresenter;
import com.sdcqjy.property.presenter.contract.SelfMsgContract;
import com.sdcqjy.property.utils.GlideUtils;
import com.sdcqjy.property.utils.ImgSelectConfig;

import java.io.File;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.finalteam.galleryfinal.FunctionConfig;
import cn.finalteam.galleryfinal.GalleryFinal;
import cn.finalteam.galleryfinal.model.PhotoInfo;

/**
 * 描述：
 * Created by rookieCi on 2017/9/21.
 */
public class SelfMsgActivity extends SimpleBaseActivity<SelfMsgPresenter> implements SelfMsgContract.View {
    @BindView(R.id.icon)
    RoundedImageView icon;
    @BindView(R.id.textName)
    TextView textName;
    @BindView(R.id.textTel)
    TextView textTel;
    @BindView(R.id.layoutChangeTel)
    LinearLayout layoutChangeTel;
    @BindView(R.id.layoutChangePwd)
    LinearLayout layoutChangePwd;
    @BindView(R.id.btnLogin)
    Button btnLogin;
    private BroadcastReceiver receiver = new BroadcastReceiver() {
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if (MyConstants.ActionLogin.equals(action)) {
                if (!intent.getBooleanExtra("login", false)) finish();
            }
        }
    };

    public static void open(Context context) {
        Intent intent = new Intent(context, SelfMsgActivity.class);
        context.startActivity(intent);
    }

    /**
     * 平移展示
     */
    public static void open(Activity activity, View icon, View name) {
        Intent intent = new Intent(activity, SelfMsgActivity.class);
        intent.putExtra(isSceneTrans, true);
        ViewCompat.setTransitionName(icon, "icon");
        ViewCompat.setTransitionName(name, "name");
        ActivityOptionsCompat compat = ActivityOptionsCompat.makeSceneTransitionAnimation(activity, new Pair<>(icon, "icon"), new Pair<>(name, "name"));
        ActivityCompat.startActivity(activity, intent, compat.toBundle());
    }

    protected int backBtn() {
        return 0;
    }

    protected void onCreate(Bundle savedInstanceState) {
        Intent intent = getIntent();
        boolean b = Build.VERSION.SDK_INT >= 21 && intent.getBooleanExtra(isSceneTrans, false);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_self_msg);
        ButterKnife.bind(this);
        presenter = new SelfMsgPresenter(this);
        if (b) {
            ViewCompat.setTransitionName(icon, "icon");
            ViewCompat.setTransitionName(textName, "name");
        }

        LoginMode mode = LoginMode.getMode(getActivity());
        GlideUtils.displayOfUrl(getActivity(), icon, mode.headImageUrl);
        findViewById(R.id.imgBack).setOnClickListener(v -> onBackPressed());

        IntentFilter intentFilter = new IntentFilter(MyConstants.ActionLogin);
        LocalBroadcastManager.getInstance(getActivity()).registerReceiver(receiver, intentFilter);
    }

    protected void onResume() {
        super.onResume();
        LoginMode mode = LoginMode.getMode(getActivity());
        if (!TextUtils.isEmpty(mode.email)) textTel.setText(mode.email);
        textName.setText(mode.userName);
    }

    protected void onDestroy() {
        super.onDestroy();
        LocalBroadcastManager.getInstance(getActivity()).unregisterReceiver(receiver);
    }

    @OnClick(R.id.icon)
    public void onIconClicked() {
        FunctionConfig squareConfig = ImgSelectConfig.getSquareConfig(getActivity(), true, 300, 300);
        GalleryFinal.openGallerySingle(0, squareConfig, new GalleryFinal.OnHanlderResultCallback() {
            public void onHanlderSuccess(int reqeustCode, List<PhotoInfo> resultList) {
                int size = resultList.size();
                if (size > 0) {
                    PhotoInfo photoInfo = resultList.get(0);
                    String photoPath = photoInfo.getPhotoPath();
                    File file = new File(photoPath);
                    GlideUtils.displayOfFile(getActivity(), icon, file);
                    updataPhoto(file);
                }
            }

            public void onHanlderFailure(int requestCode, String errorMsg) {
            }
        });
    }

    @OnClick(R.id.layoutChangeTel)
    public void onLayoutChangeTelClicked() {
        ChangeNameActivity.open(getActivity(), 1);
    }

    @OnClick(R.id.layoutChangePwd)
    public void onLayoutChangePwdClicked() {
        ChangePwdActivity.open(getActivity());
    }

    @OnClick(R.id.layoutChangeName)
    public void onLayoutChangeNameClicked() {
        ChangeNameActivity.open(getActivity());
    }

    @OnClick(R.id.btnLogin)
    public void onBtnLoginClicked() {
        PublicDialog dialog = new PublicDialog(getActivity());
        dialog.setTitle(null).setContent(R.string.tcdqzh).setTextSize(16).setOnPublicDialogClick(new PublicDialog.OnPublicDialogClick() {
            public void onConfirmClick() {
                AppLL.getAppLL().setLogin(false);
            }

            public void onCancelClick() {
            }
        }).show();
    }

    //更改头像
    private void updataPhoto(File file) {
        presenter.updataPhoto(file);
    }

    public void updataPhotoRet(String url) {
        GlideUtils.displayOfUrl(getActivity(), icon, url);
    }

    public void upAvatarRet(String url) {
        GlideUtils.displayOfUrl(getActivity(), icon, url);
        LoginMode mode = LoginMode.getMode(getActivity());
        mode.headImageUrl = url;
        LoginMode.setMode(getActivity(), mode);
    }
}
